import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import menusIcon from "./menusIcon.json"
import cityData from "./citydata"

export default new Vuex.Store({
    state: {
        token: localStorage.getItem('cms-token'),
        username: localStorage.getItem('username'),
        opened: true,
        menusIcon: menusIcon,
        cityData: cityData,
    },
    mutations: {
        setToken(state, payload) { // payload 载荷
            state.token = payload
                /*
              保存用户名和 token （持久化 用户名和token）
              Vuex 保存的数据 在 内存中，读取速度 比 localStorage 读取数据的速度快
              localStorage 保存在数据 在 磁盘上
            */
            localStorage.setItem("cms-token", payload);
        },
        removeToken(state) {
            state.token = null
            localStorage.removeItem('cms-token')
        },
        removeUsername(state) {
            state.username = null
            localStorage.removeItem('username')
        },
        setSideBarOpen(state) {
            state.opened = !state.opened
        },
        setUsername(state, payload) {
            state.username = payload
            localStorage.setItem("username", payload);
        },
    },

    actions: {

    },
    modules: {},

});